/*******************************************************************************
* Copyright (c) 2012 
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* zhangliang - initial API and implementation
*******************************************************************************/

#ifndef X_ENDIAN_H
#define X_ENDIAN_H

#if defined(__linux__)
#  include <endian.h>
#elif defined(__FreeBSD__) || defined(__NetBSD__)
#  include <sys/endian.h>
#elif defined(__OpenBSD__)
#  include <sys/types.h>
#  define be16toh(x) betoh16(x)
#  define be32toh(x) betoh32(x)
#  define be64toh(x) betoh64(x)
#endif

inline uintptr_t htobe_ptr(uintptr_t host_ptrbits) {
  return sizeof(uintptr_t) == 4 
      ? htobe32(host_ptrbits) 
      : htobe64(host_ptrbits);
}

inline uintptr_t betoh_ptr(uintptr_t big_endian_ptrbits) {
  return sizeof(uintptr_t) == 4
      ? be32toh(big_endian_ptrbits)
      : be64toh(big_endian_ptrbits);
}

#endif // X_ENDIAN_H

